//
//  InitViewController.m
//  HallOrder
//
//  Created by 蒋 浩锋 on 12-10-19.
//  Copyright (c) 2012年 蒋 浩锋. All rights reserved.
//

#import "InitViewController.h"
#import "CommonObject/ServerNetworkCenter.h"
#import "CommonObject/ClientNetworkCenter.h"
#import "Plugin/MBProgressHUD/MBProgressHUD.h"
#import "CommonObject/DataCenter.h"
#import "CommonObject/SocketDataHandle.h"
#import "RoomStatesObject.h"
#import <QuartzCore/QuartzCore.h>
#import "MTStatusBarOverlay.h"
#import "SDImageCache.h"

@interface InitViewController () <MBProgressHUDDelegate, SocketDataHandleDelegate,MTStatusBarOverlayDelegate>
{
    MBProgressHUD *HUD;
}

@end

@implementation InitViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self)
    {
        // Custom initialization
    }

    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    [[SDImageCache sharedImageCache] clearDisk];
    [SocketDataHandle sharedInstance].delegate = self;
    [[ServerNetworkCenter sharedInstance] initConnect];
    self.navigationController.navigationBarHidden = YES;
    HUD = [[MBProgressHUD alloc] initWithView:self.view];
    [self.view addSubview:HUD];
    HUD.labelText = @"正在连接服务器...";
    [HUD show:YES];
    // Do any additional setup after loading the view.
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return interfaceOrientation == UIInterfaceOrientationLandscapeLeft || interfaceOrientation == UIInterfaceOrientationLandscapeRight;
}

- (void)socketDataHandle:(SocketDataHandle *)socketHanler serverConnect:(BOOL)serverConnect
{
    if (serverConnect)
    {
        [[ServerNetworkCenter sharedInstance]sentMsg:rtSysDate :nil];
        if ([DataCenter sharedInstance].isBoundByClerk)
        {
            [[ServerNetworkCenter sharedInstance] sentMsg:rtHallBound:[DataCenter sharedInstance].UDID,@"1",@"",@"", nil];
        }
        else
        {
            [self performSegueWithIdentifier:@"showLoginVC" sender:self];
        }
    }
    else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"错误" message:@"连接服务器失败" delegate:self cancelButtonTitle:@"重试" otherButtonTitles:nil];
        alert.tag = 0;
        [alert show];
    }
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    NSLog(@"click alertview btn %d", buttonIndex);

    if (alertView.tag == 0)
    {
        [[ServerNetworkCenter sharedInstance] initConnect];
    }
}
-(void)socketDataHandleDidHallBound:(BOOL)isBound errorMsg:(NSString *)errorMsg
{
    if (isBound)
    {
        [self performSegueWithIdentifier:@"showRoonStatesVC" sender:self];
    }else
    {
        UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"" message:errorMsg delegate:self cancelButtonTitle:@"取消" otherButtonTitles:nil];
        alert.tag = 0;
        [alert show];
    }
}
@end